var blockSize = 55;
var numBubbles = 5;
var board = new Array(numBubbles);
var component;
var gameStarted = false;
var bubbleStep = 5;
var bubblesClicked = 0;
var updateTime = 200
var score = 0;
var startTime = 0;

function onUpdate()
{
    var step = 5;
    score = (new Date() - startTime)/1000;
    // %2.02f
    var p1 = Math.floor(score)
    var p2 = Math.floor(100*(score - p1))
    scoreText.text = "Time: " + p1 + "." + p2 + "  ";

    for (var n = 0; n < numBubbles; n++)
    {
        var x = board[n].x + board[n].dx;
        var y = board[n].y + board[n].dy;
        if(x > (background.width - board[n].width) || x < 0)
        {
            board[n].dx = -board[n].dx;
            x = x + 2*board[n].dx;
        }
        if(y > (background.height - board[n].height) || y < 0)
        {
            board[n].dy = -board[n].dy;
            y = y + 2*board[n].dy;
        }
        board[n].x = x;
        board[n].y = y;
    }

}

function removeBubble(mx,my)
{
    if(!gameStarted)
        return;

    for (var n = 0; n < numBubbles; n++)
    {
        if(board[n].visible)
        {
            if( (mx > board[n].x) && (mx < (board[n].x + board[n].width)) &&
                (my > board[n].y) && (my < (board[n].y + board[n].height)) )
            {
                board[n].visible = false;
                ++bubblesClicked;
                //bubblepop.play();
                break;
            }
        }
    }
    if(bubblesClicked >= numBubbles)
    {
        gameOverDialog.show("Congratulations !\n" +
                            "You popped all bubbles in\n" +
                            score + " seconds !");
        newGameBut.text = " Start "
        gameStarted = false;
        bubblesClicked = 0;
        gameTimer.stop();
    }
}

function aboutGame()
{
    aboutDialog
    aboutDialog.show("Bubble kid by:\n" +
                     "Marcelo Barros de Almeida\n" +
                     "<marcelobarrosalmeida@gmail.com>");

}

function stopGame()
{
    //Delete blocks from previous game
    for (var i = 0; i < numBubbles; i++)
    {
        if (board[i] != null)
            board[i].destroy();
    }
    newGameBut.text = " Start "
    gameStarted = false;
    bubblesClicked = 0;
    gameTimer.stop();

}

function startNewGame()
{
    if(gameStarted)
    {
        stopGame();
    }
    else
    {

        //Initialize Board
        board = new Array(numBubbles);
        for (var n = 0; n < numBubbles; n++)
        {
            board[n] = null;
            createBlock(n);
        }

        bubblesClicked = 0;
        gameTimer.start();
        gameStarted = true;
        newGameBut.text = " Stop ";
        startTime = new Date();
        score = 0;
    }
}

function createBlock(n)
{
    if (component == null)
        component = Qt.createComponent("Bubble.qml");

    // Note that if Block.qml was not a local file, component.status would be
    // Loading and we should wait for the component's statusChanged() signal to
    // know when the file is downloaded and ready before calling createObject().
    if (component.status == Component.Ready)
    {
        var obj = component.createObject(background);
        if (obj == null) {
            console.log("error creating bubble");
            console.log(component.errorString());
            return false;
        }
        obj.index = n;
        obj.width = blockSize;
        obj.height = blockSize;
        obj.x = Math.random()*(background.width-blockSize);
        obj.y = Math.random()*(background.height-blockSize);
        obj.dx = Math.random() > 0.5 ? bubbleStep : -bubbleStep;
        obj.dy = Math.random() > 0.5 ? bubbleStep : -bubbleStep;
        board[n] = obj;
    }
    else
    {
        console.log("error loading bubble component");
        console.log(component.errorString());
        return false;
    }
    return true;
}

function quitGame()
{
    Qt.quit()
}

function setGameSpeed(s)
{    
    var old = gameStarted;

    if(gameStarted)
        startNewGame();

    if(s == 0)
        updateTime = 200;
    else if(s == 1)
        updateTime = 75;
    else if(s == 2)
        updateTime = 25;

    gameTimer.interval = updateTime;

    if(old)
        startNewGame();
}

function getUpdateTime()
{
    return updateTime;
}

function setNumBubbles(n)
{
    var old = gameStarted;

    if(gameStarted)
        startNewGame();

    if(n == 0)
        numBubbles = 5;
    else if(n == 1)
        numBubbles = 10;
    else if(n == 2)
        numBubbles = 20;

    if(old)
        startNewGame();
}

function getNumBubbles()
{
    return numBubbles;
}

function getScore()
{
    if(gameStarted)
        return score;
    else
        return "--";
}
